package controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import excecoes.DadosCadastroInvalidos;

import model.basicas.CargoDepartamento;
import model.basicas.Departamento;
import model.negocios.ControladorCargo;

/**
 * Servlet implementation class CargoServlet
 */
@WebServlet("/CargoServlet")
public class CargoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CargoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String cmd = request.getParameter("cmd");
		
		if (cmd == null)
			cmd = "principal";
		CargoDepartamento cargo = new CargoDepartamento();
		ControladorCargo cargoDAO = new ControladorCargo();
		
		if(cmd != null || !cmd.equalsIgnoreCase("principal"))
		{
			
			cargo.setDescricao(request.getParameter("descricao"));
			String departamento = request.getParameter("departamento");
			if (departamento==null)
				departamento = "0";
			cargo.getDepartamento().setIdDepartamento(Integer.parseInt(departamento));
				
		}
		try{
			RequestDispatcher rd = null;
			if (cmd.equalsIgnoreCase("listar")){
				
				List<CargoDepartamento> listCargos = cargoDAO.consultarTodos();
				request.setAttribute("lista", listCargos);
				rd = request.getRequestDispatcher("/listaCargos.jsp");
			}
			else if(cmd.equalsIgnoreCase("addCargo")){
				
				cargoDAO.cadastrar(cargo);
				rd = request.getRequestDispatcher("/CargoServlet?cmd=listar");
				request.setAttribute("msg", "Cargo cadastrado!");
			}
			else if(cmd.equalsIgnoreCase("excluir")){
				
				long id = Integer.parseInt(request.getParameter("id"));
				cargo.setIdCargo(id);
				cargoDAO.remover(cargo);
				rd = request.getRequestDispatcher("/CargoServlet?cmd=listar");
				request.setAttribute("msg", "Cargo removido!");
			}
			else if(cmd.equalsIgnoreCase("att")) {
				
				long id = Integer.parseInt(request.getParameter("id"));
				cargo.setIdCargo(id);
				cargo = cargoDAO.consultarPorId(cargo.getIdCargo());
				HttpSession session = request.getSession(true);
				session.setAttribute("cargo", cargo);
				rd = request.getRequestDispatcher("/cargoFormAtt.jsp");
				
			}
			else if(cmd.equalsIgnoreCase("atualizar")){
				
				long id = Integer.parseInt(request.getParameter("id"));
				cargo.setIdCargo(id);
				cargoDAO.atualizar(cargo);
				rd = request.getRequestDispatcher("/CargoServlet?cmd=listar");
				request.setAttribute("msg", "Dados alterados!");
			}
			else if(cmd.equalsIgnoreCase("principal")){
				
				rd = request.getRequestDispatcher("/logado.jsp");
				
			} 
			rd.forward(request, response);
		}catch (DadosCadastroInvalidos e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
